﻿using com.iot.core.data;
using com.iot.core.data.entity;
using com.iot.core.exception;
using com.iot.core.infrastructure;
using com.iot.core.manager.auth;
using com.iot.core.manager.id;
using System.Reflection;

namespace com.iot.ht
{
    /*
     常用指令

    git clone https://gitee.com/gaoshan2/iotjava2023.git
    git pull origin master


     //add-migration
    //update-database
    //Remove-Migration

    npm install -g pnpm

pnpm install

启动项目

npm run dev

打包项目

npm run build

    New-Service -Name "Aht2023" -BinaryPathName "D:\APP\Aht2023\com.iot.ht.exe  --ContentRoot D:\APP\Aht2023"   -Description "Iot服务监控" -DisplayName "Aht2023" -StartupType Automatic

     */
    public class DbInit
    {
        IRepository<HtUser> baseMaper;
        public   DbInit()
        {
            baseMaper=EngineContext.Current.Resolve<IRepository<HtUser>>();
            

        }
        public void Init()
        {
            var mplat = new HtPlat
            {
                Id = 1000L,
                Name = "研发平台",
                Memo = "",
                Pid = 0,
            };
            baseMaper.DB.Add(mplat);

            var mdept = new HtDept
            {
                Id = 1000L,
                Pid = 0,
                HtareaId = 0,
                Address = "",
                CrtDid = 1000L,
                CrtDt = DateTime.Now,
                CrtUid = 1000L,
                Deleted = false,
                Dutyperson = "",
                EdtDt = DateTime.Now,
                EdtUid = 1000L,
                Gid = HtIdManager.Current.NextGuid(),
                HtplatId = 1000L,
                Lat = 0,
                Lng = 0,
                Memo = "",
                Name = "研发机构",
                RootId = 1000L,
                State = "启用",
                Tel = "",
                Url = "",

            };
            baseMaper.DB.Add(mdept);

            var mrole = new HtRole
            {
                Id = 1000L,
                Name = "研发管理",
                CrtDid = 1000L,
                Deleted = false,
                CrtDt = DateTime.Now,
                CrtUid = 1000L,
                EdtDt = DateTime.Now,
                EdtUid = 1000L,
                Gid = HtIdManager.Current.NextGuid(),
                HtplatId = 1000L,
                Memo = "研发"
            };
            baseMaper.DB.Add(mrole);


            baseMaper.DB.SaveChanges();
        }

        public  void SaveChanges()
        {
            baseMaper.DB.SaveChanges(); 
        }
        public void AddUser(long Id,string Ename,string Name,string Pwd,long HtdeptId,long HtroleId)
        {
            var muser = new HtUser
            {
                Id = Id,
                Ename = Ename,
                Name = Name,
                EdtDt = DateTime.Now,
                Address = "",
                Birthday = DateTime.Now,
                CrtDid = 1000L,
                CrtDt = DateTime.Now,
                CrtUid = 1000L,
                Pwd = AuthManager.Current.CreatePwd(Ename, Pwd),
                Deleted = false,
                IdCode = "",
                EdtUid = 1000L,
                Email = "",
                Gid = HtIdManager.Current.NextGuid(),
                HtareaId = 1L,
                Memo = "",
                Mobile = "",
                Phone = "",
                Czpwd = "",
                Sex = "",
                Url = "",
                Zw = ""
            };
            baseMaper.DB.Add(muser);

            var mhtuserdept = new HtUserDept
            {
                Id = HtIdManager.Current.NextLong(),
                CrtDid = 0,
                CrtDt = DateTime.Now,
                CrtUid = 0,
                Deleted = false,
                EdtDt = DateTime.Now,
                EdtUid = 0,
                Gid = HtIdManager.Current.NextGuid(),
                HtdeptId = HtdeptId,
                HtuserId = muser.Id,
                State = "启用",

            };
            baseMaper.DB.Add(mhtuserdept);


            var mhtuserrole = new HtUserRole
            {
                Id = HtIdManager.Current.NextLong(),
                CrtDid = 0,
                CrtDt = DateTime.Now,
                CrtUid = 0,
                Deleted = false,
                EdtDt = DateTime.Now,
                EdtUid = 0,
                Gid = HtIdManager.Current.NextGuid(),
                HtdeptId = HtdeptId,
                HtuserId = muser.Id,
                HtroleId = HtroleId,

            };
            baseMaper.DB.Add(mhtuserrole);
        }

      

        public static void   EntityToDto(object src, object dst, PropertyInfo[] srcType, PropertyInfo[] dstType)
        { 
            foreach (var f in srcType)
            {
                var f1 = dstType.Where(a=>a.Name== f.Name).FirstOrDefault();    
                if (f1 == null)
                {
                    continue;
                }
                if (f.GetType() != f.GetType())
                {
                    continue;
                }
                f1.SetValue(dst, f.GetValue(src));
            }
        }
    }
}
